﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using dsacbasic.Models;
using System.Web.Security;
using GADC.Dao;
using GADC.Domain;

namespace dsacbasic.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AdminDao dao = new AdminDao();
                Admin admin = dao.QueryByNameAndPassword(model.UserName, model.Password);
                if(admin != null)
                {
                    FormsAuthentication.RedirectFromLoginPage(model.UserName, false);

                    return Redirect(returnUrl ?? Url.Action("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "用户名密码不匹配");
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        public ActionResult Logout(string returnUrl)
        {
            //TODO 根据不同的角色定位到不同的页面
            FormsAuthentication.SignOut();

            return Redirect(returnUrl ?? Url.Action("Login", "Account"));
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AdminDao dao = new AdminDao();
                Admin admin = dao.QueryByName(model.UserName);
                if (admin != null)
                {
                    ModelState.AddModelError("", "该用户名已存在，请更换用户名");
                    return View();
                }
                else
                {
                    Admin obj = new Admin() { Name = model.UserName, Password = model.Password,IsSuper=false};
                    dao.SaveOrUpdate(obj);
                    FormsAuthentication.RedirectFromLoginPage(obj.Name, true);

                    return Redirect(Url.Action("Index", "Home"));
                }
            }
            else
            {
                return View();
            }
        }

        [Authorize]
        public ActionResult AlterPassword()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult AlterPassword(PasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                string username = User.Identity.Name;
                AdminDao dao = new AdminDao();
                Admin admin = dao.QueryByName(username);

                if (admin.Password != model.OldPassword)
                {
                    ModelState.AddModelError("", "旧密码不正确，请重试");
                    return View();
                }
                else
                {
                    admin.Password = model.Password;
                    dao.SaveOrUpdate(admin);

                    TempData["message"] = "密码修改成功";
                    return View();
                }
            }
            else
            {
                return View();
            }
        }
    }
}
